import requests
from bs4 import BeautifulSoup
import numpy as np
import pandas as pd
import operator
import re

header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'}
URL="https://nba.hupu.com/players/rockets"
html=requests.get(URL,headers=header).text
bs=BeautifulSoup(html,"html5lib")
list=bs.select(".players_left a")
p_height=[]
p_weight=[]
labels=[]
names=[]
for l in list:
    team_URL=l.get("href")
    team_html=requests.get(team_URL,headers=header).text
    team_bs=BeautifulSoup(team_html,"html5lib")
    #获取身高 体重 场上位置
    for i in range(1,11):
        name=team_bs.select(".players_table tr")[i].select("td")[1].select("b a")[0].text
        pos = team_bs.select(".players_table tr")[i].select("td")[3].text
        height=float(re.findall("\d+\.\d+", team_bs.select(".players_table tr")[i].select("td")[4].text)[0])
        weight=int(re.findall("\d+", team_bs.select(".players_table tr")[i].select("td")[5].text)[0])
        if weight !=0:
            names.append(name)
            p_height.append(height)
            p_weight.append(weight)
            labels.append(pos)

#转化为向量 要将数值归一化
h_max=max(p_height)
h_min=min(p_height)
w_max=max(p_weight)
w_min=min(p_weight)
input_height=[]
input_weight=[]
input_labels=[]
for h in p_height:
    input_height.append((h-h_min)/(h_max-h_min))
for w in p_weight:
    input_weight.append((w- w_min) / (w_max - w_min))
for l in labels:
    #位置分别有G:0 F:0.5 C:1 G-F F-G:0.25 C-F F-C:0.75
    if(operator.eq(l,"G")):
        input_labels.append(0.0)
    elif(operator.eq(l,"F")):
        input_labels.append(0.5)
    elif (operator.eq(l, "C")):
        input_labels.append(1.0)
    elif (operator.eq(l, "G-F") or operator.eq(l, "F-G")):
        input_labels.append(0.25)
    else:
        input_labels.append(0.75)


data_set={
    "name":names,
    "height":input_height,
    "weight":input_weight,
    "labels":input_labels
}
data=pd.DataFrame(data_set,columns=["name","height","weight","labels"])
data.to_csv("players_data.csv")